﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class AccessorialBLL : IBaseInfo
    {
        private static readonly IAccessorialChargesDA accessorialChargesDA = DataAccess.CreateAccessorialChargesDA();
        private static readonly IAccessorialDA AccessorialDA = DataAccess.CreateAccessorialDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IGeneralLedgerDA generalLedgerDA = DataAccess.CreateGeneralLedgerDA();
        private static readonly IOwnerDA ownerDA = DataAccess.CreateOwnerDA();
        private PubParasInfo pubParasInfo;

        public AccessorialBLL()
        {
            this.pubParasInfo = null;
        }

        public AccessorialBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public AccessorialBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> IDS)
        {
            string accessorialIDS = string.Empty;
            foreach (string id in IDS)
            {
                string where = " WHERE ACCESSORIAL_ID = '" + id + "'";
                if (accessorialChargesDA.GetAccessorialChargesByWhere(where, this.dataBaseID[0], this.pubParasInfo) != null)
                {
                    accessorialIDS = accessorialIDS + id + ";";
                }
            }
            if (!string.IsNullOrEmpty(accessorialIDS))
            {
                PublicMethod.GetInstance().ShowAlertMessage("F9180_005", this.pubParasInfo.UserLanguage, new List<string> { accessorialIDS.Substring(0, accessorialIDS.Length - 1) });
            }
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string temp = string.Empty;
            try
            {
                List<AccessorialInfo> AccessorialInfos = new List<AccessorialInfo>();
                AccessorialInfo contractInfo = null;
                foreach (string id in IDS)
                {
                    contractInfo = AccessorialDA.GetAccessorialByID(dataBase, tran, id, this.dataBaseID[0]);
                    AccessorialInfos.Add(contractInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (AccessorialDA.DeleteAccessorial(dataBase, tran, IDS, this.dataBaseID[0]) > 0)
                {
                    for (int i = 0; i < IDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<AccessorialInfo>(dataBase, tran, AccessorialInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "ACCESSORIAL", this.dataBaseID[0]);
                        temp = temp + IDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return AccessorialDA.GetAllAccessorial(this.dataBaseID[0]);
        }

        public object GetInfoByID(string id)
        {
            return AccessorialDA.GetAccessorialByID(id, this.dataBaseID[0]);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            AccessorialQueryEntity accessorialQueryEntity = obj as AccessorialQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(accessorialQueryEntity.AccessorialID))
            {
                sqlWhere.Add(" ACCESSORIAL_ID " + accessorialQueryEntity.AccessorialID);
            }
            if (!string.IsNullOrEmpty(accessorialQueryEntity.OwnerID))
            {
                sqlWhere.Add(" OWNER_ID " + accessorialQueryEntity.OwnerID);
            }
            if (!string.IsNullOrEmpty(accessorialQueryEntity.GeneralLedgerID))
            {
                sqlWhere.Add(" GENERAL_LEDGER_ID " + accessorialQueryEntity.GeneralLedgerID);
            }
            if (!string.IsNullOrEmpty(accessorialQueryEntity.BillingServiceID))
            {
                sqlWhere.Add(" BILLING_SERVICE_ID " + accessorialQueryEntity.BillingServiceID);
            }
            if (!string.IsNullOrEmpty(accessorialQueryEntity.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + accessorialQueryEntity.IsActive);
            }
            return AccessorialDA.GetAccessorialByQueryList(sqlWhere, accessorialQueryEntity, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            AccessorialInfo accessorialInfo = obj as AccessorialInfo;
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strAccessorialID = string.Empty;
            try
            {
                if (!string.IsNullOrEmpty(accessorialInfo.OwnerID) && !ownerDA.CheckTradePartnerIDUnique(dataBase, tran, accessorialInfo.OwnerID, whLoginID))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2210_003", this.pubParasInfo.UserLanguage, new List<string> { accessorialInfo.OwnerID });
                }
                if (!string.IsNullOrEmpty(accessorialInfo.GeneralLedgerID) && !generalLedgerDA.CheckGeneralLedgerIDUnique(dataBase, tran, accessorialInfo.GeneralLedgerID, whLoginID))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F9140_002", this.pubParasInfo.UserLanguage, new List<string> { accessorialInfo.GeneralLedgerID });
                }
                if (AccessorialDA.CheckAccessorialIDUnique(dataBase, tran, accessorialInfo.AccessorialID, whLoginID))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F9180_004", this.pubParasInfo.UserLanguage, new List<string> { accessorialInfo.AccessorialID });
                }
                if (string.IsNullOrEmpty(accessorialInfo.AccessorialID))
                {
                    accessorialInfo.AccessorialID = commonMethod.GenNextNumStr(dataBase, tran, "ACCESSORIAL", whLoginID);
                }
                accessorialInfo.UpdatedBy = accessorialInfo.CreatedBy;
                accessorialInfo.CreatedDate = DateTime.Now;
                accessorialInfo.UpdatedDate = DateTime.Now;
                if (AccessorialDA.InsertAccessorial(dataBase, tran, accessorialInfo, whLoginID) > 0)
                {
                    strAccessorialID = accessorialInfo.AccessorialID;
                    new AuditDataBLL().InsertAuditData<object>(dataBase, tran, null, obj, AuditAction.Insert, this.pubParasInfo.UserID, "ACCESSORIAL", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strAccessorialID;
        }

        public string Update(object obj)
        {
            AccessorialInfo accessorialInfo = obj as AccessorialInfo;
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strAccessorialID = string.Empty;
            try
            {
                if (!string.IsNullOrEmpty(accessorialInfo.OwnerID) && !ownerDA.CheckTradePartnerIDUnique(dataBase, tran, accessorialInfo.OwnerID, whLoginID))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2210_003", this.pubParasInfo.UserLanguage, new List<string> { accessorialInfo.OwnerID });
                }
                if (!string.IsNullOrEmpty(accessorialInfo.GeneralLedgerID) && !generalLedgerDA.CheckGeneralLedgerIDUnique(dataBase, tran, accessorialInfo.GeneralLedgerID, whLoginID))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F9140_002", this.pubParasInfo.UserLanguage, new List<string> { accessorialInfo.GeneralLedgerID });
                }
                AccessorialInfo oldAccessorialInfo = AccessorialDA.GetAccessorialByID(dataBase, tran, accessorialInfo.AccessorialID, this.dataBaseID[0]);
                if (AccessorialDA.UpdateAccessorial(dataBase, tran, accessorialInfo, whLoginID) > 0)
                {
                    strAccessorialID = accessorialInfo.AccessorialID;
                    new AuditDataBLL().InsertAuditData<object>(dataBase, tran, oldAccessorialInfo, obj, AuditAction.Update, this.pubParasInfo.UserID, "ACCESSORIAL", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strAccessorialID;
        }
    }
}

